/**
 * 顺序队列实现
 * 使用数组实现队列，采用循环队列的方式
 */
public class ArrayQueue<T> implements QueueInterface<T> {
    private T[] queue; // 队列数组
    private int front; // 队首指针
    private int rear; // 队尾指针
    private int maxSize; // 队列最大容量
    private int count; // 当前元素个数

    /**
     * 构造函数
     * 
     * @param capacity 队列容量
     */
    @SuppressWarnings("unchecked")
    public ArrayQueue(int capacity) {
        maxSize = capacity;
        queue = (T[]) new Object[maxSize];
        front = 0;
        rear = 0;
        count = 0;
    }

    /**
     * 默认构造函数，容量为100
     */
    public ArrayQueue() {
        this(100);
    }

    @Override
    public boolean enqueue(T item) {
        if (count == maxSize) {
            System.out.println("队列已满，无法入队");
            return false;
        }
        queue[rear] = item;
        rear = (rear + 1) % maxSize;
        count++;
        return true;
    }

    @Override
    public T dequeue() {
        if (isEmpty()) {
            System.out.println("队列为空，无法出队");
            return null;
        }
        T item = queue[front];
        queue[front] = null; // 清空引用
        front = (front + 1) % maxSize;
        count--;
        return item;
    }

    @Override
    public T front() {
        if (isEmpty()) {
            return null;
        }
        return queue[front];
    }

    @Override
    public boolean isEmpty() {
        return count == 0;
    }

    @Override
    public int size() {
        return count;
    }

    /**
     * 显示队列内容
     */
    public void display() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }

        System.out.print("队列内容: ");
        int index = front;
        for (int i = 0; i < count; i++) {
            System.out.print(queue[index] + " ");
            index = (index + 1) % maxSize;
        }
        System.out.println();
    }
}
